package dataManage;

import java.io.*; 
import java.util.*; 
import org.dom4j.*; 
import org.dom4j.io.*; 


public class readXML {
	private Vector<String> question=new Vector<String>();
	private Vector<String> answer=new Vector<String>();
	private static int index=0;
	private int itIndex;
	public readXML(String context) {
		String filename=new String();
		String pathString=new String();
		synchronized (this) {
		if(index==0){filename="paper.xml";itIndex=index;index++;}
		else{
			if(index==1){filename="paper2.xml";itIndex=index;index++;}
			else{
				filename="paper3.xml";itIndex=index;index=0;}
			}
		pathString=context+"/"+filename;
		}
		readXml(pathString);
	}
	public readXML(String context,String hisIndex) {
		String filename=new String();
		String pathString=new String();
		
		if(Integer.parseInt(hisIndex)==0){filename="paper.xml";}
		else{
			if(Integer.parseInt(hisIndex)==1){filename="paper2.xml";}
			else{
				filename="paper3.xml";}
			}
		pathString=context+"/"+filename;
		readXml(pathString);
	}
	
	public Vector<String> getQuestion(){
		return question;
	}
	public Vector<String> getAnswer(){
		return answer;
	}
	public int getItIndex(){
		return itIndex;
	}
	private void readXml(String pathString){
		SAXReader saxReader=new SAXReader();
		try{
			Document document=saxReader.read(new File(pathString));
			Element root=document.getRootElement();
			for(Iterator iter=root.elementIterator();iter.hasNext();){
				Element element=(Element)(iter.next());
				for(Iterator iterinner=element.elementIterator();iterinner.hasNext();){
					Element elementInner=(Element)(iterinner.next());
					if(elementInner.getName().equals("q")){
						question.add(elementInner.getText());
					}
					if(elementInner.getName().equals("a")){
						answer.add(elementInner.getText());
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
